//---------------------------------------------------------------------
// File: StringFormatterStep.cs
// 
// Summary: 
//
// Copyright (c) Hammersmith & Fulham Bridge Partnership. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
// PURPOSE.
//---------------------------------------------------------------------

using System;
using System.Xml;
using Microsoft.Services.BizTalkApplicationFramework.BizUnit;

namespace BizUnit.Extensions
{
	/// <summary>
	/// The StringformatterStep is used to format a string with a number of parameters and then
	/// to load the formatted string into the value of a specified context key. This is useful when
	/// you have to build say, a connection string, with dynamic values for server, database etc
	/// and then need to store in the context for use in future steps that connect to the specified database
	/// </summary>
	/// 
	/// <remarks>
	/// The following shows an example of the Xml representation of this test step.
	/// 
	/// <code escaped="true">
	///	<TestStep assemblyPath="BizUnit.Extensions.dll" typeName="BizUnit.Extensions.StringFormatterStep">
	///	<BaseString>Update table set fld = '{0}' where fld1 = '{1}'</BaseString>
	///	<Delimiter>|</Delimiter>
	///	<NumParams>2</NumParams>
	///	<ParamString>Smith|10192929239</ParamString>
	///	<ContextKey>OutputString</ContextKey>
	///	</TestStep>
	///	</code>
	///	
	///	<list type="table">
	///		<listheader>
	///			<term>Tag</term>
	///			<description>Description</description>
	///		</listheader>
	///		<item>
	///			<term>BaseString</term>
	///			<description>The base string with placeholders</description>
	///		</item>
	///		<item>
	///			<term>Delimiter</term>
	///			<description>The delimiter between the parameters that should fit into the placeholder</description>
	///		</item>
	///		<item>
	///			<term>NumParams</term>
	///			<description>The number of parameters</description>
	///		</item>
	///		<item>
	///		<term>ParamString</term>
	///		<description>The actual string with all the parameters separated by the given delimiter</description>
	///		</item>
	///		<item>
	///		<term>ContextKey</term>
	///		<description>the name of the context key in which to store the output value</description>
	///		</item>
	///	</list>
	///	</remarks>

	public class StringFormatterStep: ITestStep
	{

		/// <summary>
		/// ITestStep.Execute() implementation
		/// </summary>
		/// <param name='testConfig'>The Xml fragment containing the configuration for this test step</param>
		/// <param name='context'>The context for the test, this holds state that is passed beteen tests</param>
		public void Execute(XmlNode testConfig, Context context)
		{
			// read test config...
			string BaseString = context.ReadConfigAsString(testConfig,"BaseString");
			int numParams = context.ReadConfigAsInt32(testConfig,"NumParams");
			string ParamString = context.ReadConfigAsString(testConfig,"ParamString");
			string contextKey = context.ReadConfigAsString(testConfig,"ContextKey");
			string delim = context.ReadConfigAsString(testConfig,"Delimiter");
			
			//setup the array of parameters
			string[] ParamArray = ParamString.Split(delim.ToCharArray(),numParams);
			//now format the string
			string result = String.Format(BaseString,ParamArray);
		
			context.LogInfo(string.Format("String Formatter created : {0}", result));
			context.Add(contextKey,result,true);
			XmlNode validationConfig = testConfig.SelectSingleNode("ValidationStep");
			context.ExecuteValidator(null, validationConfig);
		}

	}
}
